package com.hand.llq25404.service.impl;

import com.hand.llq25404.entity.SsmItem;
import com.hand.llq25404.mapper.IItemMapper;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import java.util.List;

@Service
public class ExcleImpl {

    @Autowired
    private IItemMapper iItemMapper;

    public void export(String[] titles, ServletOutputStream out) throws Exception {
        try {
            // 第一步，创建一个workbook，对应一个Excel文件
            HSSFWorkbook workbook = new HSSFWorkbook();
            // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
            HSSFSheet hssfSheet = workbook.createSheet("sheet1");
            // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
            HSSFRow row = hssfSheet.createRow(0);
            // 第四步，创建单元格，并设置值表头 设置表头居中
            HSSFCellStyle hssfCellStyle = workbook.createCellStyle();
            //居中样式
            hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            HSSFCell hssfCell = null;
            hssfSheet.autoSizeColumn(1, true);
            for (int i = 0; i < titles.length; i++) {
                hssfCell = row.createCell(i);//列索引从0开始
                hssfCell.setCellValue(titles[i]);//列名1
                hssfCell.setCellStyle(hssfCellStyle);//列居中显示
            }
            hssfSheet.setColumnWidth(0, 100*256);
            hssfSheet.setColumnWidth(1, 100*256);
            hssfSheet.setColumnWidth(2, 10*256);
            hssfSheet.setColumnWidth(3, 50*256);
            hssfSheet.setColumnWidth(4, 50*256);
            hssfSheet.setColumnWidth(5, 5*256);
            // 第五步，写入实体数据
            List<SsmItem> list = iItemMapper.getSsmItems();
            for (int i = 0 ; i < list.size() ; i ++){
                row = hssfSheet.createRow(i+1);
                row.setHeightInPoints(20);
                SsmItem ssmItem = list.get(i);
                row.createCell(0).setCellValue(ssmItem.getItemCode());
                row.createCell(1).setCellValue(ssmItem.getItemDescription());
                row.createCell(2).setCellValue(ssmItem.getItemUom());
                Cell cell = row.createCell(3);
                cell.setCellValue(ssmItem.getStartActiveDate());

                CreationHelper createHelper = workbook.getCreationHelper();
                CellStyle cellStyle = workbook.createCellStyle();
                cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-MM-dd"));
                cell.setCellStyle(cellStyle);

                Cell cell1 = row.createCell(4);
                cell1.setCellValue(ssmItem.getEndActiveDate());
                CellStyle cellStyle1 = workbook.createCellStyle();
                cellStyle1.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-MM-dd"));
                cell1.setCellStyle(cellStyle1);

                row.createCell(5).setCellValue(ssmItem.getEnabledFlag());
            }
            hssfSheet.setColumnWidth(0, 3000);
            hssfSheet.setColumnWidth(1, 3000);
            hssfSheet.setColumnWidth(2, 3000);
            hssfSheet.setColumnWidth(3, 3000);
            hssfSheet.setColumnWidth(4, 3000);
            hssfSheet.setColumnWidth(5, 3000);
            // 第七步，将文件输出到客户端浏览器
            try {
                workbook.write(out);
                out.flush();
                out.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("导出信息失败！");
        }
    }


}